﻿using System;
using System.Collections.Generic;
using System.IO;
using Document.Library;
using Container.Library;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Project.Logic;
using Project.Model;
using Tool.Model;
using Web.Manage.App_Start;
using Common.Library;

namespace Web.Manage.Areas.Staff.Controllers
{
    /// <summary>
    /// 员工考勤明细操作控制器
    /// </summary>
    [Area("Staff")]
    public class ExamineController : BaseController
    {

        private IHostingEnvironment hostingEnv;
        private readonly IEmployeeService employeeService = null;//员工管理逻辑

        public ExamineController(IHostingEnvironment env)
        {
            this.hostingEnv = env;
            //员工管理逻辑注入
            employeeService = UnityCIContainer.Instance.GetService<IEmployeeService>();
        }


        /// <summary>
        /// 列表界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 详细界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Detail()
        {
            return View();
        }


        #region 查询

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadExamineList()
        {
            var resultInfo = new ResultJsonInfo<List<Dictionary<string, object>>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<ExamineQueryRequest>>();

                resultInfo = employeeService.LoadExamineList(parametersInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <returns>处理结果</returns>
        [EnableCors("AllowSpecificOrigin")]
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult EpxortExamine()
        {
            var resultInfo = new ResultJsonInfo<List<Dictionary<string, object>>>();

            FileStream fs = null;
            BinaryWriter bw = null;

            try
            {
                var parametersInfo = DeserializeRequestData<ParametersInfo<ExamineQueryRequest>>();
                resultInfo = employeeService.LoadExamineAllList(parametersInfo);

                List<string> headlist = new List<string>() { "编号", "员工姓名", "性别", "年月" };

                DateTime dtNow = Convert.ToDateTime(parametersInfo.parameters.YearMothDate);
                int days = DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
                string index = string.Empty;
                for (int i = 1; i <= days; i++)
                {
                    if (i < 10)
                    {
                        index = "0" + i.ToString();
                    }
                    else
                    {
                        index = i.ToString();
                    }
                    headlist.Add(parametersInfo.parameters.YearMothDate + "-" + index);
                }
                headlist.Add("出勤合计");

                string fileName = "员工考勤明细信息-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

                var savePath = ExcelHelper.ExportToExcelReturnFilePath<Dictionary<string, object>>(resultInfo.Data, headlist,
                    rowData =>
                {

                    List<object> content = new List<object>();
                    content.Add(rowData["UserNo"]);
                    content.Add(rowData["Name"]);
                    content.Add(EnumExtentions.GetEnumDescriptionByValue(int.Parse(rowData["Sex"].ToString()), typeof(SexType)));
                    content.Add(rowData["YearMothDate"]);
                    for (int i = 1; i <= days; i++)
                    {
                        content.Add(rowData[i.ToString()]);
                    }
                    content.Add(rowData["AttendanceTotal"]);

                    return content.ToArray();
                },
                false,
                hostingEnv.WebRootPath,
                fileName
                );
                resultInfo.Msg = savePath;
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            finally
            {
                if (fs != null) fs.Close();
                if (bw != null) bw.Close();
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 更具日期和员工编号获取指定的考情信息
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadDesignatedExamine()
        {

            var resultInfo = new ResultJsonInfo<List<T_AttendanceEntity>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<ExamineDesignatedQueryRequest>>();

                resultInfo = employeeService.LoadDesignatedExamine(parametersInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion


        #region 更新
        /// <summary>
        /// 修改具体考情信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult ModifyAttendance()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<AttendanceModifyRequest>>();

                resultInfo = employeeService.ModifyAttendance(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 考情 审核通过
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult AuditInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<AttendanceAuditRequest>>>();

                resultInfo = employeeService.AuditInfo(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        

        #endregion

        #region 删除信息
        /// <summary>
        /// 删除考情信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult RemoveAttendance()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<int>>>();

                resultInfo = employeeService.RemoveAttendance(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion
    }
}